package com.lengxf.netty.config;

import io.netty.channel.Channel;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author Lengxf
 * @version 1.0.0
 * @ClassName ChannelContextHolder.java
 * @Description TODO
 * @createTime 2022-10-10日 11:34:00
 */
public class ChannelContextHolder {

    private static NettyClient nettyClient;

    private static Channel channel;

    private final static AtomicBoolean shouldReconnect = new AtomicBoolean(false);

    public static Channel getChannel() {
        return channel;
    }

    public static void setChannel(Channel channel) {
        remove();
        ChannelContextHolder.channel = channel;
    }

    public static void remove() {
        ChannelContextHolder.channel = null;
        setShouldReconnect(true);
    }

    public static AtomicBoolean getShouldReconnect() {
        return shouldReconnect;
    }

    public static Boolean setShouldReconnect(Boolean b) {
        ChannelContextHolder.shouldReconnect.set(b);
        return getShouldReconnect().get();
    }

    public static NettyClient getNettyClient() {
        if (nettyClient == null) {
            setNettyClient(new NettyClient());
        }
        return nettyClient;
    }

    public static void setNettyClient(NettyClient nettyClient) {
        ChannelContextHolder.nettyClient = nettyClient;
    }
}
